﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using YuFeng.Models.User;
using YuFeng.Services.User;

namespace YuFeng.Web
{
    public class SessionContext
    {

        /// <summary>
        /// 当前登陆用户
        /// </summary>
        public static User CurrentUser
        {
            get
            {
                return HttpContext.Current.Session["CURRENT_USER"] as User;
            }
            set
            {
                HttpContext.Current.Session["CURRENT_USER"] = value;
            }
        }
        /// <summary>
        /// 以Cookies为准，若Cookies为空而Session不为空，则自动注销Session
        /// 若Cookies不为空，而Session为空则自动登录
        /// 如二则都不为空或者二者都为空，则不做任何处理
        /// </summary>
        public static void AutoSyncIdentity()
        {
            var currentUser = HttpContext.Current.Session["CURRENT_USER"] as User;
            // 而Form Auth Cookies为空，则自动注销;
            if (!HttpContext.Current.User.Identity.IsAuthenticated && currentUser != null)
                SessionContext.SignOut();
            var userService = DependencyResolver.Current.GetService<IUserService>();
            // Don't use SessionContext.CurrentUser in this function, because we called "AutoLoginByCookies" in Session.CurrentUser's getter.
            // 若Cookies不为空，而Session为空，则通过Cookies重新登录;
            User user;
            if (HttpContext.Current.User.Identity.IsAuthenticated && currentUser == null)
            {
                user = userService.Get(HttpContext.Current.User.Identity.Name);
                if (user != null)
                    SessionContext.SignIn(user, false);
            }
        }

        #region 是否已登录
        public static bool IsAuthenticated
        {
            get
            {
                AutoSyncIdentity();
                return HttpContext.Current.User.Identity.IsAuthenticated;
            }
        }
        #endregion

        #region 登陆、注销
        public static void SignOut()
        {
            HttpContext.Current.Session.Remove("CURRENT_USER");
            FormsAuthentication.SignOut();
        }
        public static void SignIn(User user, bool isRememberMe = false)
        {
            SignOut();

            var userService = DependencyResolver.Current.GetService<IUserService>();
            var userRoles = userService.GetUserRoles(user.Id);
            CurrentUserRoles = userRoles;

            FormsAuthentication.SetAuthCookie(user.Id, isRememberMe);
            SessionContext.CurrentUser = user;
        }
        #endregion

        #region 用户角色
        public static bool HasRole(int roleId)
        {
            if (CurrentUserRoles != null && CurrentUserRoles.Any(o => o.RoleId == roleId))
                return true;
            return false;
        }

        protected static IList<UserRole> CurrentUserRoles
        {
            get
            {
                return HttpContext.Current.Session["CURRENT_USER_ROLES"] as IList<UserRole>;
            }
            set
            {
                HttpContext.Current.Session["CURRENT_USER_ROLES"] = value;
            }
        }
        #endregion
    }
}
